<template>
    <div>
      <table-expand
        ref="table"
        :columns="columns"
        :data="basicData"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></table-expand>
      <new-add-modal
        v-bind="addNewPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleNewAddSubmit"
      ></new-add-modal>
      <edit-modal
        v-bind="editPanel"
        :modelType="modelType"
        :columns="formColumns"
        :rules="rules"
        @submit="handleEditSubmit"
      ></edit-modal>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
    </div>
</template>

  <script>
    import tableExpand from '@/pages/components/tableExpand.vue';
    import newAddModal from '@/pages/components/newAddModal.vue';
    import editModal from '@/pages/components/editModal.vue';
    import readModal from '@/pages/components/readModal.vue';
    import logicDictionaryMixin from '@/pages/components/mixins/logicDictionaryMixin'
    import columnsMixin from './config'
    import { typeClassName } from '@/pages/config/classNameMap';
    export default {
        components: { tableExpand, newAddModal, editModal, readModal },
        name: 'equipType',
        mixins: [logicDictionaryMixin,columnsMixin],
        data () {
            return {
                classObject: null, // 单个实例
                className: typeClassName['equipType'].className,
                type: typeClassName['equipType'].type,
                modelType: typeClassName['equipType'].modelType,
                equipCollectionObject: null,
                addNewPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2
                },
                editPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_code: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_name: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    category: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: true,
                    batchDeleteButton: true,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: true,
                    additionalAction: false,
                    hidePage: true
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 20, pageNumber: 1 },
                basicData: [],
                categoryOptions: [{ label: '设备', value: '0' }, { label: '仪表', value: '1' }]// 所属分类，设备和仪表
            };
        },
        computed: {
            additionalAction () {
                return [];
            }
        },
        beforeMount () {
            this.classObject = this.$modelObject.create(this.modelType);
            this.equipCollectionObject = this.$modelObject.create(typeClassName['equipment'].modelType+'Collection');
            this.loadPage();
        },
        mounted () {},
        methods: {
          // 查看框显示类别
          readDataCondition(data) {
            data.category = this.getOptionValue(this.categoryOptions, data.category)
          },
          /**
           * 删除条件
           * 设备类别，不能删除被设备使用的类别
           */
          deleteCondition(selection){
            let className = typeClassName['equipment'].className;
            let type = typeClassName['equipment'].type;
            let condition = `className=='${className}' && type=='${type}' && sf_equipmentType in (${this.combineIds (selection.map(d=>d.sf_code))})`
            return this.equipCollectionObject.query(condition).then(flag=>{
                if(flag && this.equipCollectionObject.records.length===0){
                    return true
                }else{
                    this.$Message.error('不能删除被设备使用的类别')
                    return false
                }
            })
          },
          combineIds (ids) {
              return (ids.length > 0 ? "'" + ids.join("','") + "'" : '');
          },
        }
    };
  </script>
  <style scoped lang="scss"></style>
